package com.shd.edanpei.customer.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.customer.adapter.COrderAdapter;
import com.shd.edanpei.customer.contract.CMainContract;
import com.shd.edanpei.customer.presenter.CMainPresenter;
import com.shd.edanpei.view.RefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/28.
 * yang-wen-wen
 */

public class COrderActivity extends BaseActivity<CMainPresenter>
        implements CMainContract.CMainIView {

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.refresh_layout)
    RefreshLayout refreshLayout;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;

    private COrderAdapter mAdapter;

    @Override
    protected int setLayout() {
        return R.layout.activity_corder;
    }

    @Override
    protected CMainPresenter bindPresenter() {
        return new CMainPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {

        headline.setText("订单记录");
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new COrderAdapter(this);
        recyclerView.setAdapter(mAdapter);
        mPresenter.search();
    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.search();
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt(Constant.INDEX, position);
                bundle.putInt(Constant.ORDER_ID, mAdapter.getItem(position).getOrderId());
                startActivity(COrderDetailsActivity.class, bundle);
            }
        });
    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

    @Override
    public RefreshLayout getRefreshLayout() {
        return refreshLayout;
    }

    @Override
    public COrderAdapter getAdapter() {
        return mAdapter;
    }
}
